package cn.sirun.com.fca.account.repo.register;


import com.tencent.cloud.iov.kernel.constant.ApiConst;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.kernel.service.ServiceGenerator;
import com.tencent.cloud.iov.kernel.service.interceptor.ExceptionTransformer;

import cn.sirun.com.fca.account.model.base.CaptchaRequest;
import cn.sirun.com.fca.account.model.base.ResendResponse;
import cn.sirun.com.fca.account.model.base.SubmitPasswordRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.register.RegisterSubmitResponse;
import cn.sirun.com.fca.account.model.register.SubmitEmailRequest;
import io.reactivex.Observable;

public class RegisterRepository {
    private static volatile RegisterRepository sInstance;

    public static RegisterRepository getInstance() {
        if (sInstance == null) {
            synchronized (RegisterRepository.class) {
                if (sInstance == null) {
                    sInstance = new RegisterRepository();
                }
            }
        }

        return sInstance;
    }

    private final RegisterService mRegisterService;

    private RegisterRepository() {
        mRegisterService = ServiceGenerator.createService(RegisterService.class, ApiConst.getBaseUrl());
    }

    public Observable<BaseResponse<SubmitPhoneResponse>> submitPhone(SubmitPhoneRequest request) {
        return mRegisterService.submitPhone(request)
                .compose(new ExceptionTransformer<SubmitPhoneResponse>());
    }

    public Observable<BaseResponse<RegisterSubmitResponse>> submitCaptcha(String code) {
        return mRegisterService.submitCaptcha(new CaptchaRequest(code))
                .compose(new ExceptionTransformer<RegisterSubmitResponse>());
    }

    public Observable<BaseResponse<ResendResponse>> resendCaptcha() {
        return mRegisterService.resendCaptcha()
                .compose(new ExceptionTransformer<ResendResponse>());
    }

    public Observable<BaseResponse<LoginResponse>> setPassword(String password) {
        return mRegisterService.setPassword(new SubmitPasswordRequest(password))
                .compose(new ExceptionTransformer<LoginResponse>());
    }

    public Observable<BaseResponse<SubmitPhoneResponse>> submitEmail(SubmitEmailRequest request) {
        return mRegisterService.submitEmail(request)
                .compose(new ExceptionTransformer<SubmitPhoneResponse>());
    }

    public Observable<BaseResponse<RegisterSubmitResponse>> submitEmailCaptcha(String code) {
        return mRegisterService.submitEmailCaptcha(new CaptchaRequest(code))
                .compose(new ExceptionTransformer<RegisterSubmitResponse>());
    }

    public Observable<BaseResponse<ResendResponse>> resendEmailCaptcha() {
        return mRegisterService.resend()
                .compose(new ExceptionTransformer<ResendResponse>());
    }

    public Observable<BaseResponse<LoginResponse>> setEmailPassword(String password) {
        return mRegisterService.setEmailPassword(new SubmitPasswordRequest(password))
                .compose(new ExceptionTransformer<LoginResponse>());
    }

}
